//
//  DnButton.m
//  DnRollTabbarMenu
//
//  Created by Ngoc on 05/12/2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "DnButton.h"

@interface DnButton (private)
-(void) setUp;
-(void) changeState;
- (void)recolorItemsWithColor:(UIColor *)color shadowColor:(UIColor *)shadowColor shadowOffset:(CGSize)shadowOffset shadowBlur:(CGFloat)shadowBlur;
@end

@implementation DnButton
@synthesize backgroundImg, isAutoResize;

-(void) setUp
{
    CGSize imageSize = self.backgroundImg.size;
    CGSize titleSize;
    CGFloat totalHeight;
    
    titleSize = [self.titleLabel.text sizeWithFont:self.titleLabel.font];
    totalHeight = self.frame.size.height; 
    if ( isAutoResize && titleSize.width >= self.frame.size.width) {
        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, titleSize.width + kDnButtonMargin, self.frame.size.height);
        
    }
    self.titleEdgeInsets = UIEdgeInsetsMake(
                                            0.0, - imageSize.width, -(totalHeight - titleSize.height), 0.0);
    
    [self setNeedsDisplay];
}

- (void)recolorItemsWithColor:(UIColor *)color shadowColor:(UIColor *)shadowColor shadowOffset:(CGSize)shadowOffset shadowBlur:(CGFloat)shadowBlur
{
    CGColorRef cgColor = [color CGColor];
    CGColorRef cgShadowColor = [shadowColor CGColor];
    CGRect contextRect;
    contextRect.origin.x = 0.0f;
    contextRect.origin.y = 0.0f;
    contextRect.size = self.backgroundImg.size;
    CGSize itemImageSize = [self.backgroundImg size];
    CGPoint itemImagePosition; 
    itemImagePosition.x = ceilf((contextRect.size.width - itemImageSize.width) / 2);
    itemImagePosition.y = ceilf((contextRect.size.height - itemImageSize.height) / 2);
    UIGraphicsBeginImageContext(contextRect.size);
    CGContextRef c = UIGraphicsGetCurrentContext();
    // Setup shadow
    CGContextSetShadowWithColor(c, shadowOffset, shadowBlur, cgShadowColor);
    // Setup transparency layer and clip to mask
    CGContextBeginTransparencyLayer(c, NULL);
    CGContextScaleCTM(c, 1.0, -1.0);
    CGContextClipToMask(c, CGRectMake(itemImagePosition.x, -itemImagePosition.y, itemImageSize.width, -itemImageSize.height), [self.backgroundImg CGImage]);
    // Fill and end the transparency layer
    CGContextSetFillColorWithColor(c, cgColor);
    contextRect.size.height = -contextRect.size.height;
    CGContextFillRect(c, contextRect);
    CGContextEndTransparencyLayer(c);
    // Set selected image and end context
    [self setImage:UIGraphicsGetImageFromCurrentImageContext() forState:UIControlStateSelected|UIControlStateHighlighted];
    UIGraphicsEndImageContext();
}


-(id) initWithFrame:(CGRect)frame title:(NSString *)title backgroundImage:(UIImage *)backgroundImage autoResize:(BOOL)autoResize
{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = frame;
        [self setTitle:title forState:UIControlStateNormal];
        [self setImage:backgroundImage forState:UIControlStateNormal];
        backgroundImg = backgroundImage;
        isAutoResize = autoResize;
        [self.titleLabel sizeToFit];
        
        self.backgroundColor = [UIColor clearColor];
        [self.imageView removeFromSuperview];
        [self recolorItemsWithColor:[UIColor whiteColor]shadowColor:[UIColor whiteColor] shadowOffset:CGSizeMake(0.0f, -1.0f) shadowBlur:3.0f];
        
        selectedColorBg = [[UIColor colorWithRed:74.0f/255.0f green:78.0f/255.0f blue:67.0f/255.0f alpha:1.0f]retain];
        [self performSelector:@selector(setUp) withObject:nil afterDelay:0.0f];
    }
    return  self;
}
-(void) selectedEventHandler
{
    
}
-(void) changeState
{
    [self performSelector:@selector(changeStateInner) withObject:nil afterDelay:0.0f];
}
-(void) changeSateInner
{
    if (self.selected) {
        self.selected = NO;
        self.highlighted= NO;
    }
    else
    {
        self.selected = YES;
        self.highlighted = YES;
    }
}

-(void) drawRect:(CGRect)rect
{
    CGRect drawRect = rect;
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    if (self.selected || self.highlighted) {
        CGContextSetLineWidth(context, 1.0f);	
        CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
        CGContextSetFillColorWithColor(context, selectedColorBg.CGColor);
        CGFloat radius = 5.0f;	
        CGFloat minx = CGRectGetMinX(drawRect), midx = CGRectGetMidX(drawRect), maxx = CGRectGetMaxX(drawRect);
        CGFloat miny = CGRectGetMinY(drawRect), midy = CGRectGetMidY(drawRect), maxy = CGRectGetMaxY(drawRect);	
        CGContextMoveToPoint(context, minx, midy);
        CGContextAddArcToPoint(context, minx, miny, midx, miny, radius);
        CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius);
        CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
        CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius);
        CGContextFillPath(context);
        CGContextStrokePath(context);
    }
    CGContextDrawImage(context, CGRectMake(self.frame.size.width/2-self.backgroundImg.size.width/2, 0, self.backgroundImg.size.width, backgroundImg.size.height), self.currentImage.CGImage);
}
@end
